package com.yh.csx.crm.webapi.controller;

import com.yh.csx.crm.appapi.controller.BaseController;
import com.yh.csx.crm.core.entity.CommonResponse;
import com.yh.csx.crm.core.enums.ApprovalListTypeEnum;
import com.yh.csx.crm.core.enums.ApprovalTypeEnum;
import com.yh.csx.crm.service.request.TodoListReq;
import com.yh.csx.crm.core.util.BeanUtils;
import com.yh.csx.crm.po.ApprovalAndCustomerSnapshot;
import com.yh.csx.crm.po.SysProvince;
import com.yh.csx.crm.service.ApprovalService;
import com.yh.csx.crm.service.CustomerService;
import com.yh.csx.crm.service.PublicService;
import com.yh.csx.crm.service.request.AppGetExpandInfoReq;
import com.yh.csx.crm.service.response.*;
import com.yh.csx.crm.service.vo.PageWrap;
import com.yh.csx.crm.service.vo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * AuditController class
 *
 * @author huyalin
 * @date 2019/2/20
 */
@Api("审批服务")
@RestController
@RequestMapping("/approval")
public class ApprovalController extends BaseController {

    @Autowired
    ApprovalService approvalService;

    @Autowired
    CustomerService customerService;

    @Autowired
    PublicService publicService;

    @ApiOperation("新建客户待审列表")
    @GetMapping("/new_todo")
    public CommonResponse<PageWrap<List<NewApprovalTodoListResp>>> newTodoList(TodoListReq req) {
        return super.visit(() -> {
            User user = new User(super.getUserId(), super.getUserName());
            List<String> approvalTypeCodes = new ArrayList<>();
            approvalTypeCodes.add(ApprovalTypeEnum.NEW.getCode());
            if (Objects.nonNull(req.getListType()) && req.getListType().equals(ApprovalListTypeEnum.TODO.getCode())) {
                PageWrap pageWrap = approvalService.getTodoList(user, req, approvalTypeCodes);
                List<ApprovalAndCustomerSnapshot> listResult = (List<ApprovalAndCustomerSnapshot>) pageWrap.getResult();
                List<NewApprovalTodoListResp> newApprovalTodoListResps = BeanUtils.convertList(NewApprovalTodoListResp.class, listResult);
                pageWrap.setResult(newApprovalTodoListResps);
                return pageWrap;
            } else if (Objects.nonNull(req.getListType()) && req.getListType().equals(ApprovalListTypeEnum.COMPLETED.getCode())) {
                PageWrap pageWrap = approvalService.getCompletedList(user, req, approvalTypeCodes);
                List<ApprovalAndCustomerSnapshot> listResult = (List<ApprovalAndCustomerSnapshot>) pageWrap.getResult();
                List<NewApprovalTodoListResp> newApprovalTodoListResps = BeanUtils.convertList(NewApprovalTodoListResp.class, listResult);
                pageWrap.setResult(newApprovalTodoListResps);
                return pageWrap;
            } else {
                PageWrap pageWrap = approvalService.getMyStartList(user, req, approvalTypeCodes);
                List<ApprovalAndCustomerSnapshot> listResult = (List<ApprovalAndCustomerSnapshot>) pageWrap.getResult();
                List<NewApprovalTodoListResp> newApprovalTodoListResps = BeanUtils.convertList(NewApprovalTodoListResp.class, listResult);
                pageWrap.setResult(newApprovalTodoListResps);
                return pageWrap;
            }
        });
    }


    @ApiOperation("客户信息变更待审列表")
    @GetMapping("/modify_todo")
    public CommonResponse<PageWrap<List<ModifyApprovalTodoListResp>>> modifyTodoList(TodoListReq req) {
        return super.visit(() -> {
            User user = new User(super.getUserId(), super.getUserName());
            List<String> approvalTypeCodes = new ArrayList<>();
            approvalTypeCodes.add(ApprovalTypeEnum.CREDIT.getCode());
            approvalTypeCodes.add(ApprovalTypeEnum.TEMP_CREDIT.getCode());
            approvalTypeCodes.add(ApprovalTypeEnum.MODIFY.getCode());
            approvalTypeCodes.add(ApprovalTypeEnum.EXT_COMPANY.getCode());
            if (Objects.nonNull(req.getListType()) && req.getListType().equals(ApprovalListTypeEnum.TODO.getCode())) {
                PageWrap pageWrap = approvalService.getTodoList(user, req, approvalTypeCodes);
                List<ApprovalAndCustomerSnapshot> listResult = (List<ApprovalAndCustomerSnapshot>) pageWrap.getResult();
//                List<ModifyApprovalTodoListResp> modifyApprovalTodoListRespList = BeanUtils.convertList(ModifyApprovalTodoListResp.class, listResult);
                List<ModifyApprovalTodoListResp> modifyApprovalTodoListRespList = new ArrayList<>();
                listResult.forEach(approvalAndCustomerSnapshot -> {
                    ModifyApprovalTodoListResp resp = new ModifyApprovalTodoListResp();
                    org.springframework.beans.BeanUtils.copyProperties(approvalAndCustomerSnapshot, resp);
                    resp.setApprovalNumber(approvalAndCustomerSnapshot.getApprovalNumber().toString());
                    modifyApprovalTodoListRespList.add(resp);
                });
                modifyApprovalTodoListRespList.forEach(modifyApprovalTodoListResp -> {
                    SysProvince sysProvince = publicService.getSysProvince(modifyApprovalTodoListResp.getSalesProvince());
                    modifyApprovalTodoListResp.setSalesProvince(sysProvince.getProvince());
                });
                pageWrap.setResult(modifyApprovalTodoListRespList);
                return pageWrap;
            } else if (Objects.nonNull(req.getListType()) && req.getListType().equals(ApprovalListTypeEnum.COMPLETED.getCode())) {
                PageWrap pageWrap = approvalService.getCompletedList(user, req, approvalTypeCodes);
                List<ApprovalAndCustomerSnapshot> listResult = (List<ApprovalAndCustomerSnapshot>) pageWrap.getResult();
//                List<ModifyApprovalTodoListResp> modifyApprovalTodoListRespList = BeanUtils.convertList(ModifyApprovalTodoListResp.class, listResult);
                List<ModifyApprovalTodoListResp> modifyApprovalTodoListRespList = new ArrayList<>();
                listResult.forEach(approvalAndCustomerSnapshot -> {
                    ModifyApprovalTodoListResp resp = new ModifyApprovalTodoListResp();
                    org.springframework.beans.BeanUtils.copyProperties(approvalAndCustomerSnapshot, resp);
                    resp.setApprovalNumber(approvalAndCustomerSnapshot.getApprovalNumber().toString());
                    modifyApprovalTodoListRespList.add(resp);
                });
                modifyApprovalTodoListRespList.forEach(modifyApprovalTodoListResp -> {
                    SysProvince sysProvince = publicService.getSysProvince(modifyApprovalTodoListResp.getSalesProvince());
                    modifyApprovalTodoListResp.setSalesProvince(sysProvince.getProvince());
                });
                pageWrap.setResult(modifyApprovalTodoListRespList);
                return pageWrap;
            } else {
                PageWrap pageWrap = approvalService.getMyStartList(user, req, approvalTypeCodes);
                List<ApprovalAndCustomerSnapshot> listResult = (List<ApprovalAndCustomerSnapshot>) pageWrap.getResult();
//                List<ModifyApprovalTodoListResp> modifyApprovalTodoListRespList = BeanUtils.convertList(ModifyApprovalTodoListResp.class, listResult);
                List<ModifyApprovalTodoListResp> modifyApprovalTodoListRespList = new ArrayList<>();
                listResult.forEach(approvalAndCustomerSnapshot -> {
                    ModifyApprovalTodoListResp resp = new ModifyApprovalTodoListResp();
                    org.springframework.beans.BeanUtils.copyProperties(approvalAndCustomerSnapshot, resp);
                    resp.setApprovalNumber(approvalAndCustomerSnapshot.getApprovalNumber().toString());
                    modifyApprovalTodoListRespList.add(resp);
                });
                modifyApprovalTodoListRespList.forEach(modifyApprovalTodoListResp -> {
                    SysProvince sysProvince = publicService.getSysProvince(modifyApprovalTodoListResp.getSalesProvince());
                    modifyApprovalTodoListResp.setSalesProvince(sysProvince.getProvince());
                });
                pageWrap.setResult(modifyApprovalTodoListRespList);
                return pageWrap;
            }
        });
    }

    @ApiOperation("审批过程记录(01审批单,02客户)")
    @GetMapping("/log")
    public CommonResponse<List<ApprovalFlowLogResp>> getApprovalFlowLog(Long id, String type) {
        return super.visit(() -> {
            return approvalService.getApprovalFlow(id, type);
        });
    }

    @ApiOperation("审批单详情")
    @GetMapping("/details")
    public CommonResponse<SnapshotCompareResp> approvalDetails(Long approvalId) {
        return super.visit(() -> {
            SnapshotCompareResp snapshotCompareResp = approvalService.approvalDetails(approvalId);


            return snapshotCompareResp;
        });
    }

    @ApiOperation("通过/拒绝")
    @PostMapping("/confirm")
    public CommonResponse confirm(Long approveId, Boolean confirm, String rejectReason) {
        return super.visit(() -> {
            User user = new User(super.getUserId(), super.getUserName());
            approvalService.confirm(user, approveId, confirm, rejectReason);
        });
    }

    @ApiOperation("根据客户id获得公司代码")
    @GetMapping("/expand")
    public CommonResponse<AppGetExpandInfoResp> getExpandInfo(AppGetExpandInfoReq req) {
        return super.visit(() -> {
            String companyName=req.getCompanyName();
            return approvalService.getAllCompanyCode(companyName);
        });
    }
}
